package org.example.clouddemo.user.controller.app;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.example.clouddemo.bean.common.entity.User;
import org.example.clouddemo.user.service.common.param.LoginParam;
import org.example.clouddemo.user.service.common.param.RegisterParam;
import org.example.clouddemo.user.service.common.vo.LoginResultVo;
import org.example.clouddemo.user.service.common.vo.UserAppVo;
import org.example.clouddemo.user.service.common.UserService;
import org.example.common.annotation.IgnoreAuth;
import org.example.common.auth.TokenService;
import org.example.common.bean.LoginUser;
import org.example.common.response.ApiResult;
import org.example.common.response.ApiResultCode;
import org.example.common.utils.SecurityUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 用户表 app接口
 */
// api文档标注
@Api(tags = "用户接口")
@ApiSupport(order = 1)
@RequiredArgsConstructor
@RestController("appUserController")
@RequestMapping("/api/app/user")
public class UserController {

    private final UserService userService;

    private final TokenService tokenService;

    /**
     * 需要token的需要加上 @IgnoreAuth 注解
     */
    @ApiOperation(value = "用户注册", notes = "不需要token")
    @ApiOperationSupport(order = 1)
    @PostMapping("register")
    @IgnoreAuth
    public ApiResult<Long> register(@Validated @RequestBody RegisterParam param) {
        return ApiResult.success(userService.register(param));
    }


    @ApiOperation(value = "用户登录", notes = "不需要token")
    @ApiOperationSupport(order = 2)
    @PostMapping("login")
    @IgnoreAuth
    public ApiResult<LoginResultVo> login(@Validated @RequestBody LoginParam param) {
        return ApiResult.success(userService.login(param));
    }


    @ApiOperation(value = "获取用户信息", notes = "需要token")
    @ApiOperationSupport(order = 3)
    @PostMapping("getInfo")
    public ApiResult<UserAppVo> getInfo() {
        try {
            LoginUser loginUser = SecurityUtils.getUserInfo(); //  从当前请求中获取用户信息数据
            System.out.println("获取到的登录用户信息: " + loginUser);
            
            Long userId = loginUser.getId();
            System.out.println("用户ID: " + userId);

            //获取用户信息
            UserAppVo userAppVo = userService.getInfo(userId);
            System.out.println("查询到的用户信息: " + userAppVo);

            ApiResult<UserAppVo> result = ApiResult.success(userAppVo);
            System.out.println("准备返回的结果: " + result);
            System.out.println("结果的JSON: " + com.alibaba.fastjson.JSON.toJSONString(result));
            
            return result;
        } catch (Exception e) {
            System.err.println("获取用户信息异常: " + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }


    @ApiOperation("退出登录")
    @ApiOperationSupport(order = 4)
    @PostMapping(value = "logout")
    public ApiResult<Integer> logout() {
        LoginUser loginUser = SecurityUtils.getUserInfo(); // 获取当前用户信息
        tokenService.delLoginUser(String.valueOf(loginUser.getId()), loginUser.getUuid(), LoginUser.USER_TYPE_APP); // 退出当前用户
        return ApiResult.success(1);
    }
    @PostMapping("update")
    public ApiResult<Integer> update(@RequestBody User user) {
        if (StringUtils.isEmpty(user.getId())) {
            return ApiResult.res(ApiResultCode.PARAMS_NOT_EXIST);
        }
        return ApiResult.success(userService.update(user));
    }

}
